﻿Imports System.Data
Imports MySql.Data.MySqlClient
Public Class DProducts
    Dim connector As New MySqlConnection("server=127.0.0.1;database='commerce';user='root';password='';")
    Dim command As MySqlCommand
    Dim datareader As MySqlDataReader
    Private Sub DProducts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        connector.Open()
        command = New MySqlCommand("select * from products", connector)
        datareader = command.ExecuteReader()
        While datareader.Read
            comID.Items.Add(datareader.Item("id"))
        End While
        datareader.Close()
        connector.Close()
    End Sub

    Private Sub comID_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comID.SelectedIndexChanged
        cmdDelete.Enabled = True
        connector.Open()
        command = New MySqlCommand("select * from products where id=" & comID.Text, connector)
        datareader = command.ExecuteReader()
        While datareader.Read
            lblNameDesc.Text = datareader.Item("name")
            lblManufacturerDesc.Text = datareader.Item("manufacturer")
        End While
        datareader.Close()
        connector.Close()
    End Sub

    Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDelete.Click
        connector.Open()
        command = New MySqlCommand("delete from products where id=" & comID.Text, connector)
        command.ExecuteNonQuery()
        connector.Close()
        MsgBox(lblNameDesc.Text & " deleted")
        comID.Items.Remove(comID.SelectedItem)
        cmdDelete.Enabled = False
        comID.Text = "Select an ID"
        lblNameDesc.Text = "-"
        lblManufacturerDesc.Text = "-"
    End Sub
End Class